1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
import axios from "axios";
import redis from "../../../../lib/redis";
const CONSUMET_URI = process.env.API_URI;
const API_KEY = process.env.API_KEY;
const isAscending = (data) => {
for (let i = 1; i < data.length; i++) {
if (data[i].number < data[i - 1].number) {
return false;
}
}
return true;
};
async function fetchConsumet(id, dub) {
try {
if (dub) {
return [];
}
const { data } = await axios.get(
`${CONSUMET_URI}/meta/anilist/episodes/${id}`
);
if (data?.message === "Anime not found") {
return [];
}
const array = [
{
map: true,
providerId: "gogoanime",
episodes: isAscending(data) ? data : data.reverse(),
},
];
return array;
} catch (error) {
console.error("Error fetching and processing data:", error.message);
return [];
}
}
async function fetchAnify(id) {
try {
if (!process.env.API_KEY) {
return [];
}
const { data } = await axios.get(
`https://api.anify.tv/episodes/${id}?apikey=${API_KEY}`
);
if (!data) {
return [];
}
const filtered = data.filter(
(item) => item.providerId !== "animepahe" && item.providerId !== "kass"
);
const modifiedData = filtered.map((provider) => {
if (provider.providerId === "gogoanime") {
const reversedEpisodes = [...provider.episodes].reverse();
return { ...provider, episodes: reversedEpisodes };
}
return provider;
});
return modifiedData;
} catch (error) {
console.error("Error fetching and processing data:", error.message);
return [];
}
}
export default async function handler(req, res) {
const { id, releasing = "false", dub = false } = req.query;
// if releasing is true then cache for 10 minutes, if it false cache for 1 week;
const cacheTime = releasing === "true" ? 60 * 10 : 60 * 60 * 24 * 7;
let cached;
if (redis) {
cached = await redis.get(id);
console.log("using redis");
}
if (cached) {
if (dub) {
const filtered = JSON.parse(cached).filter((item) =>
item.episodes.some((epi) => epi.hasDub === true)
);
return res.status(200).json(filtered);
} else {
return res.status(200).json(JSON.parse(cached));
}
}
const [consumet, anify] = await Promise.all([
fetchConsumet(id, dub),
fetchAnify(id),
]);
const data = [...consumet, ...anify];
if (redis && cacheTime !== null) {
await redis.set(id, JSON.stringify(data), "EX", cacheTime);
}
if (dub) {
const filtered = data.filter((item) =>
item.episodes.some((epi) => epi.hasDub === true)
);
return res.status(200).json(filtered);
}
console.log("fresh data");
return res.status(200).json(data);
}
|